package com.example.springboot1.utils;


import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 文件压缩工具类
 */
public class ZipUtils {
    /**
     * 压缩文件
     * @param bytes
     * @param out
     * @param fileName
     * @throws IOException
     */
    public static void doZip(byte[] bytes, ZipOutputStream out, String fileName) {
        try {
            ZipEntry entry = new ZipEntry(fileName);
            out.putNextEntry(entry);
            out.write(bytes);
            out.closeEntry();
        }catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("压缩文件失败");
        }
    }

    /**
     * 关闭输出流
     * @param outputStream
     */
    public static void close(OutputStream outputStream) {
        if (outputStream != null){
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
                throw new BussinessException("流关闭失败");
            }
        }
    }
}
